package vivace.model;

/**
 * Represents a musical pause
 */
public class Pause extends MusicalObject {

    private long res;

    /**
     * The character used to display a whole pause
     */
	public static final String WHOLE = "\uD834\uDD3B";
	
	/**
     * The character used to display a half pause
     */
	public static final String HALF = "\uD834\uDD3C";
	
	/**
     * The character used to display a quarter pause
     */
	public static final String QUARTER = "\uD834\uDD3D";
	
	/**
     * The character used to display a 1/8 pause
     */
	public static final String EIGHTH = "\uD834\uDD3E";
	
	/**
     * The character used to display a 1/16 pause
     */
	public static final String SIXTEENTH = "\uD834\uDD3F";
	
	/**
     * The character used to display a 1/32 pause
     */
	public static final String THIRTYSECOND = "\uD834\uDD40";
	
	/**
     * The character used to display a 1/64 pause
     */
	public static final String SIXTYFOURTH = "\uD834\uDD41";

    private boolean full;
	

    public long getRes(){
        return res;
    }


    /**
     * Wether to display the Pause as a full pause, which always should be done if the pause is of length equal to the whole bar.
     */
    public void displayAsFull(){
        full = true;
    }

    /** 
     * Constructor
     * @param res
     */
    public Pause(long res){
        this.res=res;
        length = 0;
    }

    /**
     * Constructor
     * @param length
     * @param res
     */
	public Pause(long length,long res) {
        this.res=res;
		this.length = length;
	}

	/**
	 * Sets the length of the note
	 */
    public void setLength(long l){
        length = l;
    }

    /**
     * takes in an offset, splits the current pause at the offset and returns a new Pause with the length that was chop off.
     */
    public Pause splitAfter(long offset){
        if(offset <= length){
            long temp = length;
            length = offset;
            return new Pause(temp-offset,res);
        } else {
            //System.out.println("Trying to split a pause with length" + length + "at position" +offset);
            throw new IndexOutOfBoundsException(); // Well, an offset is an index, sort of...
        }
    }
	
    /**
     * Takes in the length of the Pause to be chopped off from the beginning of the current Pause.
     * Throws an IndexOutOfBoundsException if the length parameter is greater than the length of the Pause, or smaller than 0.
     * @param length how much of the Pause to chop off.
     * @return The "chopped off" Pause.
     */
    public Pause splitBefore(long length) throws IndexOutOfBoundsException{
        if(length <= this.length && length >= 0){
            long temp = this.length;
            this.length = temp-length;
            Pause pause = new Pause(length, res);
            return pause;
        } else {
            throw new IndexOutOfBoundsException(); // Well, an offset is an index, sort of...
        }
    }

    @Override
    public String toString() {
        // TODO: An empty bar is always a whole pause long.
        float len = length; 
        // If we have a  note, and we are not in the middle or end of a note, display the  sign.

        // if this pause covers a whole bar, display it as a full pause.
        if(full) {
			return WHOLE;
		}

        if (res * 3.875 < len ){
            return WHOLE;
        } else if (res * 1.875 < len && len <= res * 3.875) {
            return HALF;
        } else if (res * 0.875 < len && len <= res * 1.875) {
            return QUARTER;
        } else if (res * 0.375 < len && len <= res * 0.875) {
            return EIGHTH;
        } else if (res * 0.125 < len && len <= res * 0.375) {
            return SIXTEENTH;
        } else if (len <= res * 0.125) {
            return THIRTYSECOND;
        } else {
            return null;
        }
    }
}

